var express = require("express")
var router = express.Router()
var User = require('../models/user') //导入模型类
var Content = require("../models/content")

//统一返回格式
var responseData

router.use(function (req, res, next) {
    responseData = {
        code: 0,
        message: ''
    }
    next()
})

//用户注册
router.post('/user/register', function (req, res, next) {
    // console.log(req.body)
    var username = req.body.username
    var password = req.body.password
    var repassword = req.body.repassword

    if (username == '') {
        responseData.code = 1
        responseData.message = "用户名不能空啊"
        res.json(responseData)
        return
    }
    if (password == '') {
        responseData.code = 2
        responseData.message = "密码不能为空"
        res.json(responseData)
        return
    }
    if (password != repassword) {
        responseData.code = 3
        responseData.message = "两次密码不一致"
        res.json(responseData)
        return
    }

    //查看数据库是不是已存在用户
    User.findOne({
        username: username
    }).then(function (userInfo) {
        // console.log(userInfo)
        if (userInfo) {
            responseData.message = "用户名被注册"
            responseData.code = 4
            res.json(responseData)
            return
        } else {
            var user = new User({
                username: username,
                password: password
            })
            return user.save()
        }
    }).then(function (newUserInfo) {
        // console.log(newUserInfo)
        responseData.message = "注册成功"
        req.cookies.set("userInfo", JSON.stringify({
            _id: newUserInfo._id,
            username: newUserInfo.username
        }))
        res.json(responseData)
    })
})

router.post('/user/login', function (req, res, next) {
    var username = req.body.username
    var password = req.body.password
    if (username == '' || password == '') {
        responseData.code = 1
        responseData.message = '密码不能为空'
        res.json(responseData)
        return
    }
    //查询用户名和密码是不是存在
    User.findOne({
        username: username,
        password: password
    }).then(function (userInfo) {
        if (!userInfo) {
            // console.log(username + "+" + password)
            // console.log(userInfo)
            responseData.code = 2
            responseData.message = "用户名或者密码错误"
            res.json(responseData)
            return
        } else {
            responseData.message = "登录成功"
            responseData.userInfo = {
                username: userInfo.username,
                _id: userInfo._id
            }
            // res.render(JSON.stringify(responseData))
            //保存cookies信息
            req.cookies.set('userInfo', JSON.stringify({
                _id: userInfo._id,
                username: userInfo.username,
                isAdmin: userInfo.isAdmin
            }))
            res.json(responseData)
            return
        }
    })
})

router.post('/user/loginOut', function (req, res, next) {
    //退出就是把cookies设置成null
    req.cookies.set('userInfo', null)
    responseData.message = "退出"
    res.json(responseData)
})


//评论接口
router.post("/comment/post", function (req, res) {
    var contentId = req.body.commentId || ""
    // console.log(contentId)
    var data = {
        code: 0,
        commentList: []
    }
    var postData = {
        username: req.userInfo.username,
        postTime: new Date(),
        content: req.body.commentContent,
    }
    //评论不空
    if (postData.content != "") {
        Content.findOne({
            _id: contentId
        }).then(function (content) {
            content.comments.push(postData)
            return content.save()
        }).then(function (newContent) {
            // responseData.message = "评论成功"
            // res.json(responseData)
            data.commentList = newContent.comments
            res.json(data)
        })
    } else {
        Content.findOne({
            _id: contentId
        }).then(function (content) {
            data.commentList = content.comments
            res.json(data)
        })
    }

})

// /每次加载文章评论
router.get("/comment", function (req, res) {
    var contentId = req.query.commentId || ""
    var data = {
        code: 0,
        commentList: []
    }
    Content.findOne({
        _id: contentId
    }).then(function (content) {
        data.commentList = content.comments
        res.json(data)
    })
})

module.exports = router